import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {HttpService} from '../../../lib/service/http.service';
import {adSystemApiUrl} from '../../../../config/api';
import {LocalStorageService} from '../../../lib/service/local-storage.service';
import {ResponseContentType} from '@angular/http/src/enums';

@Injectable()
export class OrderService {

  constructor(private httpService: HttpService, private localStorageService: LocalStorageService) {
  }

  // 类目
  getOrderList(order_no = '', // 订单编号
               nick_name = '', // 卖家昵称
               goods_name = '', // 商品名称
               order_filter = '', // 下单时间 days-7 days-15 days-30
               order_type = '0', // 订单类型 0  全部 1普通 2拼团
               type = '0', // 订单状态 0全部 1待付款 2待发货 3待收货 4交易完成 5售后
               start_time = '',
               end_time = '',
               page = 1,
               pageSize = 10) {

    let startTime = 0;
    let endTime = 0;

    if (start_time !== '') {
      startTime = new Date(Date.parse(start_time.replace(/-/g, '/'))).getTime() / 1000;
    }
    if (end_time !== '') {
      end_time += ' 23:59:59';
      endTime = new Date(Date.parse(end_time.replace(/-/g, '/'))).getTime() / 1000;
    }

    const applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    const url = `${adSystemApiUrl}/v1/member/order/list_v2` +
      `?applet_uuid=${applet_uuid}&order_no=${order_no}&nick_name=${nick_name}&goods_name=${goods_name}` +
      `&order_filter=${order_filter}&order_type=${order_type}&type=${type}&start_time=${startTime}&end_time=${endTime}&page=${page}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getOrderInfo(order_no) {
    const applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    const url = `${adSystemApiUrl}/v1/member/order/get_order_info?applet_uuid=${applet_uuid}&order_no=${order_no}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getRefundOrder(order_sub_id) {
    const applet_uuid = this.localStorageService.getNowApplet().applet_uuid;
    const url = `${adSystemApiUrl}/v1/member/order/get_refund_order?applet_uuid=${applet_uuid}&order_sub_id=${order_sub_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getRefundReason() {
    const url = `${adSystemApiUrl}/v1/applet/order/get_refund_reason`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  confirmRefund(order_sub_id) {
    const url = `${adSystemApiUrl}/v1/member/order/confirm_refund`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({
      order_sub_id: order_sub_id
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  refuseRefund(order_sub_id) {
    const url = `${adSystemApiUrl}/v1/member/order/refuse_refund?order_sub_id=${order_sub_id}`;
    return this.httpService.httpPostAuth(url, '').map(rs => rs);
  }

  // 导出
  down_xsl(order_no = '', // 订单编号
           nick_name = '', // 卖家昵称
           goods_name = '', // 商品名称
           order_filter = '', // 下单时间 days-7 days-15 days-30
           order_type = '0', // 订单类型 0  全部 1普通 2拼团
           type = '0', // 订单状态 0全部 1待付款 2待发货 3待收货 4交易完成 5售后
           start_time = '',
           end_time = '',
           page = 1,
           pageSize = 10) {
    let startTime = 0;
    let endTime = 0;

    if (start_time !== '') {
      startTime = new Date(Date.parse(start_time.replace(/-/g, '/'))).getTime() / 1000;
    }
    if (end_time !== '') {
      endTime = new Date(Date.parse(end_time.replace(/-/g, '/'))).getTime() / 1000;
    }
    const url = `${adSystemApiUrl}/v1/member/order/down_xsl`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers, responseType: 3});
    const body = JSON.stringify({
      order_no: order_no,
      nick_name: nick_name,
      goods_name: goods_name,
      order_filter: order_filter,
      order_type: order_type,
      type: type,
      start_time: startTime,
      endTime: endTime,
      page: page,
      pageSize: pageSize,
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 导出选中
  down_xsl_checked(ids) {
    const url = `${adSystemApiUrl}/v1/member/order/down_xsl`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers, responseType: 3});
    const body = JSON.stringify({
      ids: ids
    });
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  getRandomFreeOrderFreeNames(team_no) {
    const url = `${adSystemApiUrl}/v1/applet/order/getRandomFreeOrderFreeNames?team_no=${team_no}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
}
